package org.qloudgen.txn.dilithium.cluster.security;

import org.apache.ibatis.session.SqlSession;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.request.Request;
import org.qloudgen.txn.dilithium.cluster.beans.UserBean;
import org.qloudgen.txn.dilithium.cluster.common.CommonValues;
import org.qloudgen.txn.dilithium.cluster.sqlmap.ConfigNames;

public class DilithiumSession extends WebSession {

	private static final long serialVersionUID = 1L;
	private String userNo;
	
	public DilithiumSession(Request request){
		super(request); 
	}

	public boolean login(UserBean user){
		
		if (authenticate(user)) {
			this.userNo = user.getUserNo();
			return true;
		}
		return false;
	}
	
	private boolean authenticate(UserBean user) {
		SqlSession sess = CommonValues.factory.openSession();
		if (sess.selectOne(ConfigNames.CHECK_USER,user) == null ) {
			sess.close();
			return false;
		}
		sess.close();
		return true;		
	}

	public boolean isUserLoggedIn() {
		return this.userNo == null ? false : true;
	}
		
	public String getUserNo(){
		return this.userNo;
	}
}
